<?php

namespace Biz\service\activity\project;

use Biz\model\activity\BizProjectCategoryModel;
use Encore\Admin\Grid;
use Encore\Admin\Widgets\Form;
use Exception;


class BizProjectCategory extends BizProjectPageTpl
{

    const TPL_CONFIG_TPL_NAME = '合作品类';


    public function condition(): array
    {
        $pid = $this->getRequest()->input('pid', 0);
        $name = $this->getRequest()->input('name', '');
        $condition = [];
        if ($pid) $condition['pid'] = $pid;
        if (strlen($name)) $condition['name[~]'] = $name;
        return $condition;
    }

    public function getList(): array
    {
        $condition = ['ORDER' => ['id' => 'DESC'], 'LIMIT' => [$this->getOffset(), $this->getPageSize()]];
        $list = BizProjectCategoryModel::dataSelect(BizProjectCategoryModel::statusCondition() + $this->condition() + $condition);
        $format = [];
        $top = BizProjectCategoryModel::topList();
        if (empty($list)) return $format;
        foreach ($list as $v) {
            $format[] = [
                'id' => $v['id'],
                'pid' => $top[$v['pid']] ?? '-',
                'name' => $v['name'],
                'created_at_date' => $v['created_at'],
            ];
        }
        return $format;
    }

    public function getTotal(): int
    {
        return BizProjectCategoryModel::statusTotal();
    }

    public function getGrid(Grid $grid)
    {
        $grid->filter(function (Grid\Filter $filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('pid', '一级合作品类')->select(BizProjectCategoryModel::topList());
            });
            $filter->column(1 / 2, function (Grid\Filter $filter) {
                $filter->equal('name', '品类名称');
            });
            if ($this->condition()) $filter->expand();
        });
        $grid->column('id', 'ID');
        $grid->column('pid', '父级合作品类名称');
        $grid->column('name', '合作品类名称');
        $grid->column('created_at_date', '创建时间');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
        });
    }

    public function getForm(Form $form)
    {
        $form->select('pid', '父级品类')->options(BizProjectCategoryModel::topList())->help("一级品类不选择此项");
        $form->text('name', '品类名称');
    }

    public function getFormInfo($id)
    {
        return BizProjectCategoryModel::dataSelectInfo(['id' => $id]);
    }

    /**
     * @throws Exception
     */
    public function save($input)
    {
        $this->adminAuthorError();
        $name = $input['name'] ?? '';
        $id = $input['id'] ?? 0;
        $pid = $input['pid'] ?? 0;
        if ($id > 0) {
            $hasChild = BizProjectCategoryModel::dataCount(['pid' => $id]) > 0;
            if ($hasChild && $pid > 0) throw new Exception("该一级品类下存在二级品类");
        }
        $old = BizProjectCategoryModel::get('id', ['name' => $name] + BizProjectCategoryModel::statusCondition());
        if ($old && $old != $id) throw new \Exception("品类名称 [{$name}] 已经存在");
        BizProjectCategoryModel::addData(['name' => $name, 'id' => $id, 'pid' => $pid]);
    }

    /**
     * @throws Exception
     */
    public function delete($id)
    {
        $this->adminAuthorError();
        BizProjectCategoryModel::statusDelete($id);
    }

    public function breadcrumb(): array
    {
        return [
            [
                'text' => BizProjectProject::getListTitle(), 'url' => BizProjectProject::getRoute()->getListRoute()
            ],
        ];
    }
}